package netssl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

public class J_ShowCertificate {

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.home"));

        String ks_name;
        if (args.length<1){
            ks_name ="F:\\temp\\newmark.cer";
        }else {
            ks_name = args[0];
        }
        try {
            FileInputStream fis = new FileInputStream(ks_name);
            BufferedInputStream bis = new BufferedInputStream(fis);
            CertificateFactory cf  = CertificateFactory.getInstance("X.509");
            if (bis.available()>0){
                Certificate cert = cf.generateCertificate(bis);
                X509Certificate xcert = (X509Certificate)cert;
                System.out.println("证书内容：");
                System.out.println("版本号："+xcert.getVersion());
                System.out.println("序列号："+xcert.getSerialNumber().toString(16));
                System.out.println("所有者："+xcert.getSubjectX500Principal());
                System.out.println("发照者："+xcert.getIssuerX500Principal());
                System.out.println("有效期开始时间："+xcert.getNotBefore());
                System.out.println("有效期结束时间："+xcert.getNotAfter());
                System.out.println("算法签名："+xcert.getSigAlgName());
                byte[] sig = xcert.getSignature();
                System.out.println("签名："+(new BigInteger(sig)).toString(16));
                byte[] k = xcert.getPublicKey().getEncoded();
                System.out.println("公钥："+(new BigInteger(k)).toString(16));
                bis.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
